/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package stendcontrol;

import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author minaev
 */
public class DataSensor implements Serializable{

    private String name;
    private ArrayList<Param> params = new ArrayList<Param>();
    private ArrayList<Func> funcs = new ArrayList<Func>();

    public DataSensor(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void addParam(Param param) {
        params.add(param);
    }

    public void removeAllParam() {
        params.clear();
    }
    
    public void removeAllParam(int type) {
        for (Param pr : params) {
            if (pr.getType() == type) {
                params.remove(pr);
            }
        }
    }

    /**
     *
     * @param nparam
     * @return
     */
    public Param getParam(int nparam) {
        if (nparam >= 0 && nparam < params.size()) {
            return params.get(nparam);
        } else {
            StendLogger.log(StendLogger.MSG_ERROR, "Bad index for getParam in "
                    + "DataSensor. nparam:" + nparam);
            return null;
        }
    }
    
    public Param getParamByType(int type){
        int i=0;
        Param tmpp;
        while(i<getCountParams()){
            tmpp = getParam(i);
            if(tmpp.getType() == type){
                return tmpp;
            }
            i++;
        }
        return null;
    }
    
    public Param getParamByType(int type,int num){
        int i=0;
        int cnt=0;
        Param tmpp;
        while(i<getCountParams()){
            tmpp = getParam(i);
            if(tmpp.getType() == type){
                if(cnt==num){
                    return tmpp;
                }else{
                    cnt++;
                }
            }
            i++;
        }
        return null;
    }

    public int getCountParams() {
        return params.size();
    }
    
    public int getIndexParam(Param prm){
        return params.indexOf(prm);
    }

    public  synchronized void addFunc(Func func) {
        funcs.add(func);
    }

    public synchronized void removeAllFunc() {
        funcs.clear();
    }

    public synchronized void removeAllFunc(int type) {
        
        for (int i=0; i<funcs.size(); i++) {
            if (funcs.get(i).getType() == type) {
                funcs.remove(i);
            }
        }
    }

    public synchronized Func getFunc(int nfunc) {
        if (nfunc >= 0 && nfunc < funcs.size()) {
            return funcs.get(nfunc);
        } else {
            StendLogger.log(StendLogger.MSG_ERROR, "Bad index:" + nfunc 
                    + " for getFunc in DataSensor");
            return null;
        }
    }
    
    public synchronized  Func getFuncByType(int type){
        int i=0;
        Func tmpf;
        while(i<getCountFuncs()){
            tmpf = getFunc(i);
            if(tmpf.getType() == type){
                return tmpf;
            }
            i++;
        }
        return null;
    }
    
    public synchronized  Func getFuncByType(int type,int num){
        int i=0;
        int cnt=0;
        Func tmpf;
        while(i<getCountFuncs()){
            tmpf = getFunc(i);
            if(tmpf.getType() == type){
                if(cnt==num){
                    return tmpf;
                }else{
                    cnt++;
                }
            }
            i++;
        }
        return null;
    }

    public boolean containTypeFunc(int type) {
        for (Func fc : funcs) {
            if (fc.getType() == type) {
                return true;
            }
        }
        return false;
    }
    
    public boolean containTypeParam(int type) {
        for (Param fc : params) {
            if (fc.getType() == type) {
                return true;
            }
        }
        return false;
    }

    public int getCountFuncs() {
        return funcs.size();
    }
    
    public synchronized  int getIndexFunc(Func fc){
        return funcs.indexOf(fc);
    }

    @Override
    public String toString() {
        return name;
    }
    
    
}
